package com.moyun.zbmy.youxian.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.moyun.zbmy.youxian.R;
import com.moyun.zbmy.youxian.application.Cache;
import com.moyun.zbmy.youxian.base.BaseActivity;
import com.moyun.zbmy.youxian.beans.FocusColumn;
import com.moyun.zbmy.youxian.beans.MediaCircleBean;
import com.moyun.zbmy.youxian.beans.MediaVo;
import com.moyun.zbmy.youxian.http.AbsListNetCallback;
import com.moyun.zbmy.youxian.http.AbsNetCallBack;
import com.moyun.zbmy.youxian.http.NetUtils;
import com.moyun.zbmy.youxian.ui.activities.H5NormalActivity;
import com.moyun.zbmy.youxian.ui.adapter.MediaCircleAdapter;
import com.moyun.zbmy.youxian.utils.ListTypeUtils;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 媒体圈
 */
public class MediaCircleFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, MediaCircleAdapter.OnMediaItemClickListener {
    @BindView(R.id.fm_live_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mediaCircleList;

    private MediaCircleAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_media_circle, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    @Override
    public void onRefresh() {
        requestData();
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        mediaCircleList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mediaCircleList.setItemAnimator(new DefaultItemAnimator());
        adapter = new MediaCircleAdapter();
        adapter.setMediaItemClickListener(this);
        mediaCircleList.setAdapter(adapter);
        refreshLayout.autoRefresh();
    }

    /**
     * 请求媒体圈数据
     */
    private void requestData() {
        NetUtils.getNetAdapter().getMediaList(getOwnerName(), channelUrl, new AbsNetCallBack<MediaVo>(MediaVo.class) {
            @Override
            public void onSuc(MediaVo vo) {
                if(null!=vo) {
                    setData(vo.getMeitiList());
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                setData(null);
            }

            @Override
            public void onEnd() {
                super.onEnd();
                closeProgress();
                refreshLayout.refreshComplete();
            }
        });

    }

    /**
     * 初始化列表
     *
     * @param mediaList
     */
    private void setData(List<MediaCircleBean> mediaList) {
        if (ListUtils.isListValued(mediaList)) {
            adapter.setData(mediaList);
        }
    }

    @Override
    public void onMediaItemClick(MediaCircleBean item) {
        Intent intent = new Intent(getContext(), H5NormalActivity.class);
        intent.putExtra(BaseActivity.EX_HTML, item.getH5Url());
        startActivity(intent);
    }
}
